// ==UserScript==
// @name         rarbt种子助手
// @namespace    http://git.oschina.net/baOZe/userscript
// @version      1.0
// @updateURL http://git.oschina.net/baOZe/userscript/raw/master/rarbt.user.js?dir=0&filepath=rarbt.user.js
// @description  rarbt的网站，从详情页到下载种子页需要点击2次才能下载，并且有弹出窗口，非常不方便。使用这个脚本只需点击原链接下方的子节点就可下载种子。
// @author       baOZe
// @match        http://www.rarbt.com/subject/*
// @grant        none
// @run-at       document-ready
// ==/UserScript==

(function() {
    'use strict';
    
    var downTorrent = function(url,args){
        //http://www.rarbt.com/index.php/dow/index.html?id=23765&zz=1
        var body = $(document.body),
            form = $("<form method='post'></form>"),
            input;
        form.attr({"action":url});
        $.each(args,function(key,value){
            input = $("<input type='hidden'>");
            input.attr({"name":key});
            input.val(value);
            form.append(input);
        });

        form.appendTo(document.body);
        form.submit();
        document.body.removeChild(form[0]);
    };
    
    var getUrlParam = function(url,name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = url.substr(url.indexOf("?") + 1).match(reg);
        if (r!==null) return unescape(r[2]); return null;
    };
    
    $('div.tinfo').each(function(i,item){
        var a = $(item).find("a");
        var url = a[0].href;
        var id = getUrlParam(url,"id");
        var zz = getUrlParam(url,"zz");
        var span = $(item).find("ul.btTree.treeview li span");
        $(span).css("cursor","pointer");
        $(span).click(function(){
            downTorrent(url,{id:id,zz:"zz"+zz});
        });
    });
    
    //downTorrent("http://www.rarbt.com/index.php/dow/index.html?id=23765&zz=1",{id:23765,zz:"zz1"});
    
})();